<?php


namespace App\backend\controllers\goods;


use App\backend\controllers\AdminBaseController;
use App\backend\models\GoodsGroupModel;
use App\backend\services\GoodsGroupServices;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Utils\JsonResp;

class GoodsGroupController extends AdminBaseController
{
    protected $class_name = 'GoodsGroup';

    /**
     * 商品分组列表
     */
    public function getList(Request $request)
    {
        $data = $request->all();
        $validator = Validator::make(
            $data,
            GoodsGroupModel::rules(),
            GoodsGroupModel::message()
        );
        if ($validator->fails()) {
            $msg = $validator->errors()->all();
            return JsonResp::error($msg[0]);
        }
        $list = GoodsGroupServices::getList($data);
        return JsonResp::success('成功', $list);
    }

    /**
     * 添加/编辑商品分组
     */
    public function goodsGroupModify(Request $request)
    {
        $data = $request->all();
        $validator = Validator::make(
            $data,
            GoodsGroupModel::modify_rules(),
            GoodsGroupModel::message()
        );
        if ($validator->fails()) {
            $msg = $validator->errors()->all();
            return JsonResp::error($msg[0]);
        }
        $list = GoodsGroupServices::goodsGroupModify($data);
        return JsonResp::success('成功', $list);
    }

    /**
     * 删除商品分组
     */
    public function goodsGroupDelete(Request $request)
    {
        $data = $request->all();
        $validator = Validator::make(
            $data,
            GoodsGroupModel::updated_rules(),
            GoodsGroupModel::message()
        );
        if ($validator->fails()) {
            $msg = $validator->errors()->all();
            return JsonResp::error($msg[0]);
        }
        $list = GoodsGroupServices::updateGoodsGroupStatus($data);
        return JsonResp::success('操作成功！', $list);
    }

    /**
     * 商品分组详情
     */
    public function goodsGroupInfo(Request $request)
    {
        $data = $request->all();
        $validator = Validator::make(
            $data,
            GoodsGroupModel::info_rules(),
            GoodsGroupModel::message()
        );
        if ($validator->fails()) {
            $msg = $validator->errors()->all();
            return errorJson($msg[0], []);
        }
        $list = GoodsGroupServices::goodsGroupInfo($data);
        return successJson('操作成功！', $list);
    }

}
